<template>
  <div class="top">
    <div class="name">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown @command="commandEvent">
      <div class="other">
        <span>操作</span>
        <i class="el-icon-arrow-down el-icon--right" />
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="!istop" command="look">查看部门</el-dropdown-item>
          <el-dropdown-item v-if="!istop" command="del">删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDelete } from '@/api/departments.js'
export default {
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    istop: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('addEvent', true, this.item, 'add')
          break
        case 'look':
          this.$bus.$emit('addEvent', true, this.item, 'edit')
          break
        case 'del':
          this.$confirm(`您确定删除[${this.item.name}]部门吗？`, '提示')
            .then(async() => {
              await companyDepartmentDelete(this.item.id)
              this.$message.success('删除成功')
              // 刷新父级列表数据
              this.$emit('getData')
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: center;
  .name {
    flex: 1;
  }
  .manager {
    width: 80px;
    margin-right: 20px;
  }
}
</style>
